package com.xp.tinyurl.service;

import com.github.pagehelper.PageHelper;
import com.xp.tinyurl.common.cache.AppCache;
import com.xp.tinyurl.dao.AppRegisterDao;
import com.xp.tinyurl.model.Register;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author liuxw
 * @date 2019/3/28
 * @since 1.0
 */
@Service
public class AppRegisterService {

    @Autowired
    private AppRegisterDao appRegisterDao;

    @Autowired
    private AppCache appCache;

    public Integer save(Register entity) {
        entity.setDeleted(false);
        entity.setCreateTime(new Date());
        entity.setModifyTime(entity.getCreateTime());
        return appRegisterDao.insert(entity);
    }

    public Integer updateByAppKey(Register entity) {
        Example example = new Example(Register.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("appKey", entity.getAppKey());
        return appRegisterDao.updateByExampleSelective(entity, example);
    }

    public List<Register> select(Register entity){
        return appRegisterDao.select(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    public Integer updateAppInfoAndCache(Register register) {
        register.setModifyTime(new Date());
        Integer res = this.updateByAppKey(register);
        appCache.add(register.getAppKey(), register);
        return res;
    }

    public List<Register> getPageList(Map<String, Object> map) {
        Example example = new Example(Register.class);
        Example.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(map.get("module"))){
            criteria.andEqualTo("module", map.get("module"));
        }
        if(!StringUtils.isEmpty(map.get("businessLine"))){
            criteria.andEqualTo("businessLine", map.get("businessLine"));
        }
        example.setOrderByClause(" create_time desc");
        if(!StringUtils.isEmpty(map.get("pageSize")) && !StringUtils.isEmpty(map.get("startPage"))){
            PageHelper.startPage((Integer) map.get("startPage"), (Integer) map.get("pageSize"));
        }
        return appRegisterDao.selectByExample(example);
    }

    public Integer getPageListCount(Map<String, Object> map) {
        Example example = new Example(Register.class);
        Example.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(map.get("module"))){
            criteria.andEqualTo("module", map.get("module"));
        }
        if(!StringUtils.isEmpty(map.get("businessLine"))){
            criteria.andEqualTo("businessLine", map.get("businessLine"));
        }
        return appRegisterDao.selectCountByExample(example);

    }

    public Register getAppRegisterByAppKey(String appKey) {
        Example example = new Example(Register.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("appKey", appKey);
        List<Register> registerList = appRegisterDao.selectByExample(example);
        return CollectionUtils.isEmpty(registerList) ? null : registerList.get(0);
    }

    public Register getAppRegisterByModule(String module) {
        Example example = new Example(Register.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("module", module);
        List<Register> registerList = appRegisterDao.selectByExample(example);
        return CollectionUtils.isEmpty(registerList) ? null : registerList.get(0);
    }

}
